<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseAdminController;
use App\Lib\Common;
use Illuminate\Http\Request;
use App\Service\BasicService;
use App\Service\PostService;
class BasicMemberController extends BaseAdminController {
    protected $basicService;
    protected $postService;

    public function _init(){
        $this->basicService = new BasicService();
        $this->postService = new PostService();
    }

    public function index(Request $request){
        $search = $request->input();
        $data = $this->basicService->getBasicMemberList(isset($search['limit'])?$search['limit']:15,$search);
        $basic = $this->basicService->getBasicName($search['basic_id']);
        $batch = $this->basicService->getBatchName($search['batch_id']);
        return view("admin.basic_member.index",compact(['data','basic','batch']));
    }

    public function create(Request $request){
        if($request->isMethod("GET")){
            $search = $request->input();
            $basic = $this->basicService->getBasicName($search['basic_id']);
            $batch = $this->basicService->getBatchName($search['batch_id']);
            return view("admin.basic_member.create",[
                'basic'=>$basic,
                'batch'=>$batch
            ]);
        }else{
            $data = $request->except("_token");
            $data['group'] = explode(',',$data['group']);
            $result = $this->basicService->createUser($data);
            return $result;
        }
    }
    public function destroy(Request $request) {
        $search = $request->input();
        $result = $this->basicService->deleteUser($search);
        if($result){
            return $this->success($result, "删除成功");
        }else{
            return $this->error(7000, "删除失败");
        }

    }
    public function condition(Request $request){
        if($request->isMethod("GET")){
            $search = $request->input();
            $post = $this->postService->selectSitePost();
            $org = Common::getTree(getData('OrgDataTag'));
            $user_group = $this->basicService->setModel("Admin.UserGroup")->getAll();
            $user = $this->basicService->getUserList($search);
            $basic = $this->basicService->getBasicName($search['basic_id']);
            $batch = $this->basicService->getBatchName($search['batch_id']);
            return view("admin.basic_member.condition",[
                'post'=>$post,
                'org'=>json_encode($org),
                'user'=>$user,
                'user_group'=>$user_group,
                'basic'=>$basic,
                'batch'=>$batch
            ]);
        }else{
            $data = $request->except("_token");
            $result = $this->basicService->createUser($data);
            if($result){
                $res['code'] = 0;
                $res['msg'] = "添加成功";
            }else{
                $res['code'] = -1;
                $res['msg'] = "添加失败";
            }
            return $res;
        }

    }
}
